import server from './server'
import handle from './handler'

server.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    token && (config.headers['Authorization'] = token)
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

server.interceptors.response.use(async res => {
  const resCode = res.status
  const fn = handle[resCode + '']
  fn && fn()
  const code = res.data.code
  const resHandle = handle[code + '']
  const handleRes = await resHandle(res.data)
  return handleRes
})

export default server
